package com.longbig.multifunction.service;

import com.longbig.multifunction.convert.SwitchConvert;
import com.longbig.multifunction.dto.GecAccountInfo;
import com.longbig.multifunction.dto.GecAccountInfoDO;
import com.longbig.multifunction.mapper.SysGeoAccountInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Zhangp
 * @date 2024/4/25 15:47
 */
@Service
@Slf4j
public class SysGeoAccountInfoService {

    @Autowired
    private SysGeoAccountInfoMapper sysGeoAccountInfoMapper;

    @Autowired
    private SwitchConvert switchConvert;


    public boolean registerUser(GecAccountInfo gecAccountInfo) {
        return sysGeoAccountInfoMapper.registerUser(gecAccountInfo);
    }

    public boolean importAccountInfoBatch(List<GecAccountInfo> gecAccountInfoList) {
        try {
            List<GecAccountInfoDO> gecAccountInfoDOList = Lists.newArrayList();
            for (GecAccountInfo gecAccountInfo : gecAccountInfoList) {
                //过滤脏数据,省市区为空，address长度大于100, 手机号长度大于11，联系人姓名大于50
                if (StringUtils.isEmpty(gecAccountInfo.getProvince()) || StringUtils.isEmpty(gecAccountInfo.getCity())
                        || StringUtils.isEmpty(gecAccountInfo.getCounty())
                        || (StringUtils.isNotBlank(gecAccountInfo.getAddress()) && gecAccountInfo.getAddress().length() > 100 )
                        || (StringUtils.isNotBlank(gecAccountInfo.getUserName()) && gecAccountInfo.getUserName().length() > 50 )
                        || (StringUtils.isNotBlank(gecAccountInfo.getUserTel()) && gecAccountInfo.getUserTel().length() > 11 )) {
                    continue;
                }

                GecAccountInfoDO gecAccountInfoDO = switchConvert.accountInfo2DO(gecAccountInfo);
                gecAccountInfoDO.setAccountCode(gecAccountInfo.getAccountCode().toUpperCase());
                gecAccountInfoDO.setRecordStatus(1);
                gecAccountInfoDO.setSyncStatus(1);
                gecAccountInfoDO.setLzSyncStatus(1);
                gecAccountInfoDO.setAccountSources(1);
                gecAccountInfoDO.setSysCode("[\"00QZQ\"]");
                //企业类型  1:事业单位2:国有企业3:国有控股企业4:民营企业5:合资企业6:其他
                gecAccountInfoDO.setCompanyType(getAccountTypeFromExcel(gecAccountInfo.getCompanyType1()));
                // 行业类型
                gecAccountInfoDO.setIndustryType(1);
                gecAccountInfoDOList.add(gecAccountInfoDO);
            }

            List<GecAccountInfoDO> filteredList = gecAccountInfoDOList.stream()
                    .collect(Collectors.toMap(
                            GecAccountInfoDO::getAccountCode,
                            accountInfo -> accountInfo,
                            (existing, replacement) -> existing,
                            LinkedHashMap::new
                    )).values().stream().collect(Collectors.toList());

            // 批量插入或更新
            int BATCH_SIZE = 500;
            int size = filteredList.size();
            log.info("过滤之后的总数为：{}", size);
            List<GecAccountInfoDO> subList;

            for (int i = 0; i < size; i += BATCH_SIZE) {
                int end = Math.min(i + BATCH_SIZE, size);
                subList = filteredList.subList(i, end);
                log.info("批量插入或更新数据:{}", subList.size());
                sysGeoAccountInfoMapper.insertAccountInfoBatch(subList);
            }

            return true;
        } catch (Exception e) {
            log.error("批量插入/更新数据报错如下：" + e.getMessage());
            return false;
        }
    }

    /**
     * 企业类型
     * 1:事业单位2:国有企业3:国有控股企业4:民营企业5:合资企业6:其他
     */
    private Integer getAccountTypeFromExcel(String accountType1) {
        if (accountType1.contains("国有控股")) {
            return 3;
        } else if (accountType1.contains("国有")) {
            return 2;
        } else if (accountType1.contains("私营") || accountType1.contains("公司")) {
            return 4;
        } else {
            return 3;
        }
    }
}
